/**
 * Random Name Generator
 * Written by drworm (Michael Atkinson)
 * Contact: michael.r.atkinson@gmail.com
 *
 * Takes one or more input files as command-line arguments
 * Outputs a random word from each file to stdout
 * Order of words corresponds to order of input files
 * i.e. output is "[word from file1, word from file2, ...]"
 * 
 * PROTIP: Replace the word "name" anywhere in the code with whatever's in yo files.
 *
 * This software is free as in speech and free as in beer
 */

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc, char **argv)
{
    if(argc == 1)
    {
        cerr << "ERROR: You must input at least one file" << endl;
        cerr << "USAGE: namegen.exe file1 [file2 file3 ...]" << endl;
        exit(1);
    }

    /**
     * FILE I/O
     * 
     * Open all input files
     * Read all words into respective lists
     * Close all input files
     */

    int numFiles = argc - 1;
    vector<ifstream> in; // input files
    in.resize(numFiles);

    for(int i = 0; i < numFiles; i++)
    {
        in[i].exceptions(ifstream::failbit | ifstream::badbit);
    }

    int errct = 0; // count number of file input errors
    for(int i = 0; i < numFiles; i++)
    {
        try
        {
            in[i].open(argv[i + 1], ifstream::in);
        }
        catch(ifstream::failure e)
        {
            cerr << "ERROR: failed to open file " << argv[i + 1] << ": " << e.what() << endl;
            errct++;
        }
    }

    if(errct >= numFiles)
    {
        cerr << "ERROR: failed to open any files, quitting..." << endl;
        exit(2);
    }

    vector<vector<string> > words; // lists of words from input files
    words.resize(numFiles);
    char word[32];
    for(int i = 0; i < numFiles; i++)
    {
        while(in[i].good())
        {
            try
            {
                in[i].getline(word, 32);
                words[i].push_back((string)word);
            }
            catch(ifstream::failure e)
            {
                //cerr << "ERROR: failed to read word from file " << argv[i + 1] << ": " << e.what() << endl;
            }
        }
    }   

    for(int i = 0; i < numFiles; i++)
    {
        try
        {
            in[i].close();
        }
        catch(ifstream::failure e)
        {
            cerr << "ERROR: failed to close file " << argv[i + 1] << ": " << e.what() << endl;
        }
    }

    /** END FILE I/O */

    /**
     * RANDOM NAME GENERATION
     * 
     * Prints one word from each list
     * i.e. one word from each file
     */

    cout << "Press Enter for a new combination." << endl;
    cout << "Press Q, then Enter to quit." << endl;
    cout << "IMPORTANT: Wait at least one second between iterations!" << endl;
    while(1)
    {
        switch(cin.get())
        {
        case '\n':
            int j;
            srand(unsigned(time(NULL)));
            for(int i = 0; i < numFiles; i++)
            {
                j = rand() % words[i].size();
                cout << words[i][j] << " ";
            }
            cout << endl;
            break;
        case 'q':
            exit(0);
            break;
        default:
            cout << "Press Enter for a new combination." << endl;
            cout << "Press Q, then Enter to quit." << endl;
            break;
        }
    }
    /** END RANDOM NAME GENERATION */

    return 0;
}